<template>
  <div class="container">
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { getQueryString } from '@/assets/js/common'
export default {
  name: 'login',
  computed: {
    ...mapState(['userNum'])
  },
  mounted () {
    this.init()
  },
  methods: {
    init () {
      let code = this.$route.query.code || getQueryString('code')
      let redirect = this.$route.query.redirect || getQueryString('redirect')
      // alert(redirect)
      if (redirect) sessionStorage.setItem('redirect', redirect)
      if (code) {
        this.getUserNum(code)
      } else {
        location.replace(this.$config.wx.link)
      }
    },
    getUserNum (code) {
      console.log('>>>获取微信用户信息')
      this.$http({
        url: `/vank/receipt/user_info1.do`,
        data: { code }
      }).then(res => {
        let data = JSON.parse(res.data)
        console.log('data>>>', data)
        if (+data.errcode !== 0) {
          console.log('获取微信个人信息失败')
          if (+data.errcode === 40029) {
            // window.location.href = this.$config.wx.link
            location.replace(this.$config.wx.link)
          } else {
            console.log(data.errmsg)
            this.$tools.popup({
              tpl: 'alert',
              title: '提示',
              content: '获取个人信息失败',
              buttons: [`<div class="tc-main">确定</div>`],
              buttonCallBack: index => {
                wx.closeWindow()
              }
            })
            this.$tools.openToast({
              msg: data.errmsg,
              duration: 2000
            })
          }
        } else {
          if (data.userid) {
            console.log('获取微信个人信息成功')
            let { userid, avatar } = data
            if (userid === 'yfmsc' || userid === 'v-meisc') {
              userid = 'zhaozm01'
              alert('梅小西的账号，切换成志敏的数据')
              console.log('梅署春的账号，自动切换成志敏的号')
            }
            this.$store.commit('userNum', userid)
            this.loginEAS()
          } else {
            this.$tools.openToast({
              msg: '请先关注公众号',
              duration: 2000
            })
            setTimeout(() => {
              wx.closeWindow()
            }, 1000)
          }
        }
      })
    },
    loginEAS () {
      this.$http({
        url: this.$config.api.host,
        data: {
          method: 'loginEAS',
          userName: 'sysWS',
          password: 'sysWS',
          slnName: 'eas',
          dcName: 'eas750',
          language: 'L2',
          dbType: '1'
        }
      }).then(res => {
        if (res.code === 200) {
          let sessionID = res.msg
          this.$store.commit('sessionID', sessionID)
          this.getUserInfo()
        }
      })
    },
    getUserInfo () {
      console.log('>>>获取费控用户信息')
      console.log('this.userNum', this.userNum)
      this.$http({
        url: this.$config.api.host,
        data: {
          method: 'getAccountBank',
          userNum: this.userNum
        }
      }).then(res => {
        if (res.code === 200) {
          let userInfo = res
          if (userInfo.depName) userInfo.depName = userInfo.depName.split('_').pop()
          this.$store.commit('userInfo', userInfo)
          this.$store.commit('userNum', userInfo.userNum)
          let redirect = sessionStorage.getItem('redirect') || '/'
          this.$router.replace(redirect)
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
@import '../assets/css/config';
@import '../assets/css/mixins';
</style>
